package com.syqc.monitor.ui.realalarm

import android.widget.ImageView
import android.widget.TextView
import androidx.activity.viewModels
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import autodispose2.autoDispose
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.syqc.comlib.base.BaseRecycleActivity
import com.syqc.comlib.custom.MLoadingDialog
import com.syqc.comlib.custom.MToolbar
import com.syqc.comlib.rx.doAfterChanged
import com.syqc.comlib.utils.ScreenUtil
import com.syqc.comlib.utils.ToolbarHelper
import com.syqc.comlib.utils.loadGif
import com.syqc.comlib.utils.startActivityTo
import com.syqc.custom.LeftRightTextView
import com.syqc.entity.AlarmDetail
import com.syqc.monitor.R
import com.syqc.monitor.databinding.ActivityAlarmSearchInfoBinding
import com.syqc.utils.Keys
import com.syqc.utils.ToolsUtil
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers

/**
 *@Author Jasper
 *@Time   2020/12/1
 *@Desc   报警类型列表
 */
class AlarmInfoActivity2 :
    BaseRecycleActivity<ActivityAlarmSearchInfoBinding>(ActivityAlarmSearchInfoBinding::inflate) {
    private lateinit var alarmId: String
    private lateinit var typeName: String
    private lateinit var num: String
    private lateinit var mToolbar: MToolbar

    private val mAdapter by lazy { MAdapter() }
    private val mViewModel by viewModels<AlarmInfoVM>()

    private val emptyView by lazy {
        val view = layoutInflater.inflate(
            R.layout.layout_empty_view,
            mRecyclerVB.recyclerView,
            false
        )
        view.findViewById<ImageView>(R.id.ivNoData).loadGif(this, R.mipmap.ic_empty)

        view
    }

    override fun initView() {
        super.initView()
        MLoadingDialog.clear()
        typeName = intent.getStringExtra(Keys.INTENT_TITLE) ?: ""
        num = intent.getStringExtra(Keys.INTENT_TEAM_NUM) ?: ""
        alarmId = intent.getStringExtra(Keys.INTENT_ALARM_ID) ?: ""
        mToolbar = ToolbarHelper.setMToolbar(this, "$typeName($num)")

        mRecyclerVB.recyclerView.layoutManager = LinearLayoutManager(this)
        mRecyclerVB.recyclerView.setPadding(0, ScreenUtil.dp2px(this, 12), 0, 0)
        mRecyclerVB.recyclerView.adapter = mAdapter

        initViewModel()

    }

    fun initViewModel() {
        mViewModel.toastObserver().observeOn(AndroidSchedulers.mainThread())
            .autoDispose(scopeProvider).subscribe {
                toastObserver(it)
            }

        mViewModel.alarms.observe(this) {
            if (it.isNullOrEmpty()) {
                mAdapter.setEmptyView(emptyView)
            }
            mAdapter.setList(it)
            mToolbar.setCenterTitle("$typeName(${it.size})")
        }

        mViewModel.start.observe(this) {
            startActivityTo<MediaInfoActivity>(this) {
                putExtra(Keys.INTENT_TITLE, mViewModel.plate)
                putExtra(Keys.INTENT_CAR_ID, mViewModel.catId)
                putParcelableArrayListExtra(Keys.INTENT_DATA, it)
            }
        }
        if (alarmId.isEmpty()) return
        mViewModel.getAlarmByType(alarmId)
    }

    override fun setListener() {

        binding.ivClear.setOnClickListener {
            binding.editSearch.setText("")
        }

        binding.editSearch.doAfterChanged {
            mViewModel.search(it.toString())
            binding.ivClear.isVisible = !it.isNullOrEmpty()
        }

        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            if (ToolsUtil.isFastClick()) return@setOnItemChildClickListener
            val mEntity = mAdapter.data[position]
            mViewModel.catId = mEntity.carId
            mViewModel.plate = mEntity.carPlate
            mViewModel.getAlarmInfo(mEntity.time, mEntity.alarmType)
        }
    }
}

class MAdapter :
    BaseQuickAdapter<AlarmDetail, BaseViewHolder>(R.layout.item_alarm_headle_info) {
    init {
        addChildClickViewIds(R.id.ivInfo)
    }

    override fun convert(holder: BaseViewHolder, item: AlarmDetail) {
        item.run {
            holder.setText(R.id.tv_alarm_info_car, carPlate)
                .setText(R.id.tv_alarm_info_address, addr)
            holder.getView<LeftRightTextView>(R.id.lr_tv_car_name)
                .setRightText(carName)
            holder.getView<LeftRightTextView>(R.id.lr_tv_alarm_belong)
                .setRightText(teamName ?: "")
            holder.getView<LeftRightTextView>(R.id.lr_tv_alarm_info_time)
                .setRightText(time)
            holder.getView<LeftRightTextView>(R.id.lr_tv_alarm_info_speed)
                .setRightText("$speed ${context.getString(R.string.unit_speed)}")
            holder.setVisible(R.id.ivInfo, !fjNum.isNullOrEmpty() && fjNum.toInt() > 0)

            holder.getView<TextView>(R.id.tvHandle).isVisible = false
        }
    }
}
